package cisdi.mes.sm.rm.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cisdi.mes.sm.rm.service.MatLoadMgr;
import cisdi.mes.sm.rm.service.MatUnloadMgr;

@Controller
public class MatMoveCtl {

	@Autowired
	private MatUnloadMgr matUnloadMgr;
	
	@Autowired
	private MatLoadMgr matLoadMgr;
	
	//下料
	@RequestMapping("/rm/MatMoveCtl/matUnload")
	@ResponseBody
	public Map<String, Object> matUnload(HttpServletRequest request) {
		// 获取浏览器端的输入
		String measureId = request.getParameter("measureId");
		String to = request.getParameter("toStorageId");
		String totalWgtStr = request.getParameter("totalWgt");
		String weightStr = request.getParameter("weight");
		
		// 定义输出
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 对输入数据进行处理
			double weight = Double.parseDouble(weightStr);
			double totalWgt = Double.parseDouble(totalWgtStr);
			map = matUnloadMgr.matUnload(measureId, to, weight,totalWgt,  request);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", e.toString());
		}
		return map;
	}

	//上料
	@RequestMapping("/rm/MatMoveCtl/loadBin")
	@ResponseBody
	public Map<String, Object> loadBin(HttpServletRequest request) {
		// 获取浏览器端的输入
		String from = request.getParameter("from");// 从料仓号
		String to = request.getParameter("to");// 到料仓号
		String weightStr = request.getParameter("weight");
		// 定义输出
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 对输入数据进行处理
			Double weight = Double.parseDouble(weightStr);
			map = matLoadMgr.loadBin(from, to, weight,  request);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", e.toString());
		}

		return map;
	}

	// 领料
	@RequestMapping("/rm/MatMoveCtl/loadManual")
	@ResponseBody
	public Map<String, Object> loadManual(HttpServletRequest request) {
		// 获取浏览器端的输入
		String from = request.getParameter("from");// 从料仓库存号
		String to = request.getParameter("to");// 到料仓号
		String weightStr = request.getParameter("weight");
		// 定义输出
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 对输入数据进行处理
			Double weight = Double.parseDouble(weightStr);
			map = matLoadMgr.loadBin(from, to, weight,  request);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", e.toString());
		}

		return map;
	}

	// 退料
	@RequestMapping("/rm/MatMoveCtl/undoMoveBat")
	@ResponseBody
	public Map<String, Object> undoMoveBat(HttpServletRequest request) {
		// 获取浏览器端的输入
		String contentIdsList = request.getParameter("contentIds");// 料仓库存

		// 定义输出
		Map<String, Object> mapBat = new HashMap<String, Object>();
		try {
			// 对输入数据进行处理
			String[] contentIds = contentIdsList.split(",");

			// 批量返回的消息
			int totalCount = contentIds.length; // 总条数
			int successCount = 0; // 成功条数
			String msgBat = ""; // 消息

			Map<String, Object> singleRslt; // 定义每条计量单的返回消息
			for (int i = 0; i < totalCount; i++) {
				Long contentId = Long.parseLong(contentIds[i]);
				singleRslt = matLoadMgr.undoMove(contentId,  request);
				if (singleRslt.get("success").toString().equals("false")) {
					String singleMsg = singleRslt.get("message").toString();
					msgBat = msgBat + "<br>*失败的库存：" + contentIds[i];
					msgBat = msgBat + "<br>*失败原因：" + singleMsg;
					break;
				} else {
					successCount++;
					msgBat = msgBat + "成功的库存：" + contentIds[i];
				}
			}

			// 汇总
			if (successCount == totalCount) {
				mapBat.put("success", true);
				mapBat.put("message", "全部成功退料");
			} else {
				mapBat.put("success", false);
				mapBat.put("message", msgBat);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mapBat.put("success", false);
			mapBat.put("message", e.toString());
		}

		return mapBat;
	}

}
